﻿<%@ WebHandler Language="C#" Class="xinxi_shuiwenliao_pandian" %>

using System;
using System.Web;
using KeaoBase;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using ServiceStack.Redis;

public class xinxi_shuiwenliao_pandian : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.Form["uid"];
            string kid = context.Request.Form["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("请重试...");
                return;
            }
            string requestType = context.Request.Form["requestType"];
            if (string.IsNullOrEmpty(requestType))
            {
                context.Response.Write("请重试...");
                return;
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {
                switch (requestType)
                {
                    case "1":
                        string zhanDianCondition = string.Format("{0}", context.Request.Form["zhandian"]);
                        string riQiCondition_kaishi = string.Format("{0}", context.Request.Form["pandianriqi_kaishi"]);
                        string riQiCondition_jieshu = string.Format("{0}", context.Request.Form["pandianriqi_jieshu"]);
                        object[] SearchParmas = new object[] {
                            m_DB.CreateParamter("@zhandian",zhanDianCondition,false),
                            m_DB.CreateParamter("@pandianriqi_kaishi",string.Format("{0} 00:00:00",riQiCondition_kaishi),false),
                            m_DB.CreateParamter("@pandianriqi_jieshu",string.Format("{0} 23:59:59",riQiCondition_jieshu),false)};

                        DataSet Ds = m_DB.ExecuteTableProcedure("P_XinXi_ShuiWenLiao_PanDian", SearchParmas);
                        if (Ds != null && Ds.Tables.Count > 0 && Ds.Tables[0].Rows.Count > 0)
                        {
                            for (int i = 0; i < Ds.Tables[0].Rows.Count; i++)
                            {
                                if (i == Ds.Tables[0].Rows.Count - 1)
                                {
                                    foreach (DataRow heJiRow in Ds.Tables[1].Rows)
                                    {
                                        if (heJiRow["wuliaomingcheng"].Equals(Ds.Tables[0].Rows[i]["wuliaomingcheng"]) && heJiRow["xinghaomingcheng"].Equals(Ds.Tables[0].Rows[i]["xinghaomingcheng"]))
                                        {
                                            DataRow newRow = Ds.Tables[0].NewRow();
                                            newRow["xinghaomingcheng"] = "合计：";
                                            newRow["benqijinchang"] = heJiRow["benqijinchang"];
                                            newRow["benqixiaohao"] = heJiRow["benqixiaohao"];
                                            i++;
                                            Ds.Tables[0].Rows.InsertAt(newRow, i);
                                            break;
                                        }
                                    }
                                    break;
                                }

                                if (!Ds.Tables[0].Rows[i]["wuliaomingcheng"].Equals(Ds.Tables[0].Rows[i + 1]["wuliaomingcheng"]) || !Ds.Tables[0].Rows[i]["xinghaomingcheng"].Equals(Ds.Tables[0].Rows[i + 1]["xinghaomingcheng"]))
                                {
                                    foreach (DataRow heJiRow in Ds.Tables[1].Rows)
                                    {
                                        if (heJiRow["wuliaomingcheng"].Equals(Ds.Tables[0].Rows[i]["wuliaomingcheng"]) && heJiRow["xinghaomingcheng"].Equals(Ds.Tables[0].Rows[i]["xinghaomingcheng"]))
                                        {
                                            DataRow newRow = Ds.Tables[0].NewRow();
                                            newRow["xinghaomingcheng"] = "合计：";
                                            newRow["benqijinchang"] = heJiRow["benqijinchang"];
                                            newRow["benqixiaohao"] = heJiRow["benqixiaohao"];
                                            i++;
                                            Ds.Tables[0].Rows.InsertAt(newRow, i);
                                            break;
                                        }
                                    }
                                }
                            }

                            string returnJson = KeaoUtils.Dt2Json(Ds.Tables[0]);
                            context.Response.Write(returnJson);
                        }
                        else
                        {
                            context.Response.Write("[]");
                        }
                        break;
                    case "2":
                        string pandianriqi = string.Format("{0} 23:59:59", context.Request.Form["pandianriqi"]);
                        string wuliao = string.Format("{0}", context.Request.Form["wuliao"]);
                        string xinghao = string.Format("{0}", context.Request.Form["xinghao"]);
                        string wuliaomingcheng = string.Format("{0}", context.Request.Form["wuliaomingcheng"]);
                        string xinghaomingcheng = string.Format("{0}", context.Request.Form["xinghaomingcheng"]);
                        string benqikucun = string.Format("{0}", context.Request.Form["benqikucun"]);
                        string pandianID = string.Format("{0}", context.Request.Form["shuiwenliaopandianID"]);
                        string zhandian = string.Format("{0}", context.Request.Form["zhandian"]);
                        if (string.IsNullOrEmpty(pandianID))
                        {
                            //新增
                            TableRow newRow = new TableRow("tbl_pandian", Guid.NewGuid().ToString(), DBCtl.插入);
                            TableCol col1 = new TableCol();
                            col1.ColName = "jieshuriqi";
                            col1.ValueMember = pandianriqi;
                            col1.ValueType = DataValueCommonType.日期时间;
                            newRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol();
                            col2.ColName = "wuliao";
                            col2.ValueMember = wuliao;
                            col2.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col2);
                            TableCol col3 = new TableCol();
                            col3.ColName = "wuliaomingcheng";
                            col3.ValueMember = wuliaomingcheng;
                            col3.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol();
                            col4.ColName = "xinghao";
                            col4.ValueMember = xinghao;
                            col4.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol();
                            col5.ColName = "xinghaomingcheng";
                            col5.ValueMember = xinghaomingcheng;
                            col5.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol();
                            col6.ColName = "benqikucun";
                            col6.ValueMember = benqikucun;
                            col6.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol();
                            col7.ColName = "zhandian";
                            col7.ValueMember = zhandian;
                            col7.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col7);
                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("保存盘点信息成功，点击查询刷新数据。");
                            }
                        }
                        else
                        {
                            //修改
                            TableRow editRow = new TableRow("tbl_pandian", pandianID, DBCtl.修改);
                            TableCol col1 = new TableCol();
                            col1.ColName = "jieshuriqi";
                            col1.ValueMember = pandianriqi;
                            col1.ValueType = DataValueCommonType.日期时间;
                            editRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol();
                            col2.ColName = "wuliao";
                            col2.ValueMember = wuliao;
                            col2.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col2);
                            TableCol col3 = new TableCol();
                            col3.ColName = "wuliaomingcheng";
                            col3.ValueMember = wuliaomingcheng;
                            col3.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol();
                            col4.ColName = "xinghao";
                            col4.ValueMember = xinghao;
                            col4.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol();
                            col5.ColName = "xinghaomingcheng";
                            col5.ValueMember = xinghaomingcheng;
                            col5.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol();
                            col6.ColName = "benqikucun";
                            col6.ValueMember = benqikucun;
                            col6.ValueType = DataValueCommonType.数字;
                            editRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol();
                            col7.ColName = "zhandian";
                            col7.ValueMember = zhandian;
                            col7.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col7);
                            if (m_DB.SaveTableRows(editRow) > 0)
                            {
                                context.Response.Write("保存盘点信息成功，点击查询刷新数据。");
                            }
                            else
                            {
                                context.Response.Write("保存失败，请重试...");
                            }
                        }
                        break;
                    case "3":
                        //删除
                        string delID = string.Format("{0}", context.Request.Form["pandianID"]);
                        TableRow delRow = new TableRow();
                        delRow.TableName = "tbl_pandian";
                        delRow.ID = delID;
                        if (m_DB.DeleteRowByID(delRow) > 0)
                        {
                            context.Response.Write("OK");
                        }
                        else
                        {
                            context.Response.Write("删除失败，请重试...");
                        }
                        break;
                }
            }

        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("盘点异常。#{0}", err.Message));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}